package cn.xkai.exercise.a;

/**
 * @description: 最大连续1的个数
 * 自己的思路：慢指针记录起始值，快制作记录结束值，差值为连续为1时的长度
 * 借鉴的思路：贪心法-遇到1则加1，否则置为0
 * 心得：...被学习标题双指针概览先入为主!!!
 * @author: kaixiang
 * @date: 2022-06-27
 **/
public class Solution16 {
    public int findMaxConsecutiveOnes(int[] nums) {
        int max = 0, slow = 0, fast = 0;
        while (slow < nums.length) {
            if (max >= nums.length - slow) {
                break;
            }
            if (nums[slow] == 1) {
                fast = slow + 1;
                while (fast < nums.length) {
                    if (nums[fast] != 1) {
                        break;
                    }
                    fast++;
                }
            }
            if (max < fast - slow) {
                max = fast - slow;
                slow = fast;
            } else {
                slow++;
            }
        }
        return max;
    }

    public int findMaxConsecutiveOnesRefer(int[] nums) {
        int max = 0, temp = 0;
        for (int num : nums) {
            if (num == 1) {
                temp++;
            } else {
                temp = 0;
            }
            max = Math.max(max, temp);
        }
        return max;
    }

    public static void main(String[] args) {
        int[] nums = {1, 0};
        Solution16 solution16 = new Solution16();
        System.out.println(solution16.findMaxConsecutiveOnes(nums));
    }
}
